<!DOCTYPE html>
<html lang="zh-CN">


<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, user-scalable=no">
  <title>
    Redis | 风林火山
  </title>
  <meta name="description" content="hello world">
  
  <meta name="keywords" content="
  
  ">
  
  <meta name="author" content="矫情">

  <meta http-equiv="Cache-Control" content="no-transform"/>
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="theme-color" content="#1e2327">
  <link rel="apple-touch-icon" href="https://github.githubassets.com/apple-touch-icon.png">
  <link rel="apple-touch-icon" sizes="180x180" href="https://github.githubassets.com/apple-touch-icon-180x180.png">

  <link rel="icon" type="image/x-icon" href="https://github.githubassets.com/favicon.ico">
  <link rel="stylesheet" href="/blog/css/main.css">
  <link rel="stylesheet"
        href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  

  

  <script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.25-csp/vue.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
</head>

<body id="replica-app">

<nav class="navbar-wrapper">
  <div class="navbar">
    <div class="container clearfix">
      <a href="/blog/" class="navbar-logo"><i class="fa fa-github"></i></a>

      <div class="navbar-search float-left desktop-only">
        <div class="navbar-search-form">
          <label for="gsc-i-id1">This website</label>
          <div id="google-search">
            <gcse:search></gcse:search>
          </div>
        </div>
      </div>

      <ul class="navbar-nav float-left">
        
        <li><a href="/blog/archives">Archives</a></li>
        
        
        <li><a href="/blog/categories">Categories</a></li>
        
        
        <li><a href="/blog/tags">Tags</a></li>
        
        
        <li class="desktop-only"><a href="/blog/atom.xml" target="_blank">RSS</a></li>
        
      </ul>

      <ul class="navbar-nav user-nav float-right desktop-only">
        <li class="user-nav-notification">
          <a><span class="user-nav-unread"></span><i class="fa fa-bell"></i></a>
        </li>
        <li>
          <a><i class="fa fa-plus"></i> <i class="fa fa-caret-down"></i></a>
        </li>
        <li class="user-nav-logo">
          <a><img src="https://octodex.github.com/images/baracktocat.jpg"> <i class="fa fa-caret-down"></i></i></a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<div class="main-container">
  <header class="header-wrapper desktop-only">
  <div class="container header-site-detail">
    <ul class="header-toolbar">
      <li class="clearfix">
        <a href="/blog/archives" class="header-toolbar-left"><i
                  class="fa fa-file-text"></i> Posts </a>
        <a href="/blog/archives"
           class="header-toolbar-right"> 5 </a>
      </li>
      <li>
        <a href="/blog/tags" class="header-toolbar-left"><i
                  class="fa fa-tags"></i> Tags </a>
        <a href="/blog/tags"
           class="header-toolbar-right"> 0 </a>
      </li>
      <li>
        <a href="/blog/categories" class="header-toolbar-left"><i
                  class="fa fa-folder-open"></i> Categories </a>
        <a href="/blog/categories"
           class="header-toolbar-right"> 0 </a>
      </li>
    </ul>
    <h2 class="header-title">
      <i class="fa fa-book text-muted"></i>
      <a href="/blog/">风林火山</a>
      
      
    </h2>
  </div>

  <div class="container">
    <div class="header-tab-wrapper clearfix">
      <span class="header-tab header-tab-selected"><i class="fa fa-thumbs-o-up"></i> Like</span>
      <span class="header-tab"><i class="fa fa-share-alt"></i> Share</span>
      <span class="header-tab"><i class="fa fa-comments-o"></i> Discussion</span>
      <span class="header-tab"><i class="fa fa-bookmark-o"></i> Bookmark </span>
      <span class="header-tab"><i class="fa fa-smile-o"></i> Smile <i class="fa fa-caret-down"></i></span>
    </div>
  </div>
</header>


<div class="post-container container">
  <h3>
    <i class="fa fa-user-o"></i>
    矫情

    <span class="post-date float-right" title="{{moment(1572072750692).format('MMM DD, YYYY, h:mm:ss A')}}">
      <i class="fa fa-pencil-square-o"></i>
      {{moment(1572072750692).fromNow()}}
    </span>
  </h3>

  <article class="post-content">
    <h1>Redis</h1>
    <h1 id="redis"><a href="#redis" class="headerlink" title="redis"></a>redis</h1><p>参考文章</p>
<blockquote>
<p><a href="https://blog.csdn.net/hellozpc/article/details/81267030#29Rediscli_99" target="_blank" rel="noopener">https://blog.csdn.net/hellozpc/article/details/81267030#29Rediscli_99</a></p>
</blockquote>
<h2 id="开发中的需求"><a href="#开发中的需求" class="headerlink" title="开发中的需求"></a>开发中的需求</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在开发网站的时候如果有一些数据在短时间之内不会发生变化，而它们还要被频繁的访问，为了提高用户的请求速度和降低网站的负载，就把这些数据放到一个读取速度更快的介质上（或者是通过较少的计算量就可以获得该数据），该行为就称作对该数据的缓存。该介质可以是文件、数据库、内存，内存经常用于数据缓存。</span><br></pre></td></tr></table></figure>



<h2 id="redis是什么"><a href="#redis是什么" class="headerlink" title="redis是什么"></a>redis是什么</h2><p>百度百科</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库，并提供多种语言的API。从2010年3月15日起，Redis的开发工作由VMware主持。从2013年5月开始，Redis的开发由Pivotal赞助。</span><br></pre></td></tr></table></figure>

<h2 id="优势"><a href="#优势" class="headerlink" title="优势"></a>优势</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Redis跟memcache不同的是，储存在Redis中的数据是持久化的，断电或重启后，数据也不会丢失。因为Redis的存储分为内存存储、磁盘存储和log文件三部分，重启后，Redis可以从磁盘重新将数据加载到内存中，这些可以通过配置文件对其进行配置，正因为这样，Redis才能实现持久化。</span><br><span class="line"></span><br><span class="line">Redis支持主从模式，可以配置集群，这样更利于支撑起大型的项目，这也是Redis的一大亮点。</span><br><span class="line"></span><br><span class="line">- 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。</span><br><span class="line">- 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。</span><br><span class="line">- 原子 – Redis的所有操作都是原子性的，同时Redis还支持对几个操作全并后的原子性执行。</span><br><span class="line">- 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。</span><br></pre></td></tr></table></figure>

<h1 id="Redis-环境搭建"><a href="#Redis-环境搭建" class="headerlink" title="Redis  环境搭建"></a>Redis  环境搭建</h1><h2 id="Windows-环境"><a href="#Windows-环境" class="headerlink" title="Windows 环境"></a>Windows 环境</h2><p>运行服务端 </p>
<p>这时候另启一个cmd窗口，原来的不要关闭，不然就无法访问服务端了。 </p>
<p>切换到redis目录下运行 redis-cli.exe 。</p>
<p>C:\redis&gt;redis-cli.exe</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; ping</span><br><span class="line"></span><br><span class="line">PONG</span><br></pre></td></tr></table></figure>

<p>以上说明我们已经成功安装了redis。 </p>
<h2 id="linux-环境"><a href="#linux-环境" class="headerlink" title="linux 环境"></a>linux 环境</h2><h3 id="上传文件"><a href="#上传文件" class="headerlink" title="上传文件"></a>上传文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">目录位置为 /usr/local/</span><br></pre></td></tr></table></figure>

<h3 id="解压文件"><a href="#解压文件" class="headerlink" title="解压文件"></a>解压文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf redis-5.0.4.tar.gz</span><br></pre></td></tr></table></figure>

<p>进入目录并编译</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd redis-5.0.4</span><br><span class="line">make</span><br></pre></td></tr></table></figure>

<h3 id="完成安装"><a href="#完成安装" class="headerlink" title="完成安装"></a>完成安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">进入src 目录</span><br><span class="line">cd src </span><br><span class="line">make install</span><br></pre></td></tr></table></figure>

<h3 id="统一管理配置文件和常用命令"><a href="#统一管理配置文件和常用命令" class="headerlink" title="统一管理配置文件和常用命令"></a>统一管理<strong>配置文件和常用命令</strong></h3><p>创建两个新文件夹</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir /usr/local/redis/etc</span><br><span class="line">mkdir /usr/local/redis/bin</span><br></pre></td></tr></table></figure>

<p>移动配置文件到etc</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mv /usr/local/redis-5.0.4/redis.conf /usr/local/redis/etc/</span><br></pre></td></tr></table></figure>

<p>移动常用命令到bin (进入到src 目录中)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/local/redis-5.0.4/src</span><br><span class="line">mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server /usr/local/redis/bin/</span><br></pre></td></tr></table></figure>

<h3 id="配置redis-后台启动"><a href="#配置redis-后台启动" class="headerlink" title="配置redis 后台启动"></a>配置redis 后台启动</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/local/redis/etc/</span><br><span class="line">vim redis.conf</span><br></pre></td></tr></table></figure>

<p>修改 daemonize 属性的值为 yes</p>
<h3 id="启动-redis-服务"><a href="#启动-redis-服务" class="headerlink" title="启动 redis 服务"></a>启动 redis 服务</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf</span><br></pre></td></tr></table></figure>

<h3 id="查看端口号"><a href="#查看端口号" class="headerlink" title="查看端口号"></a>查看端口号</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep redis</span><br></pre></td></tr></table></figure>

<p>配置redis 服务开机启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/rc.local</span><br></pre></td></tr></table></figure>

<p>添加配置文件内容为 redis 的启动命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">touch /var/lock/subsys/local</span><br><span class="line">/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf</span><br></pre></td></tr></table></figure>

<h3 id="允许外部机器连接"><a href="#允许外部机器连接" class="headerlink" title="允许外部机器连接"></a>允许外部机器连接</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">（1）修改redis.conf 文件，把bind 127.0.0.1 ::1这一行注释掉。</span><br><span class="line">（2）修改redis.conf 文件，protected-mode 要设置成no</span><br><span class="line">（3）启动的时候，需要指定redis.conf 文件，执行./src/redis-server redis.conf命令</span><br></pre></td></tr></table></figure>

<h3 id="redis-重启服务"><a href="#redis-重启服务" class="headerlink" title="redis 重启服务"></a>redis 重启服务</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -h 127.0.0.1 -p 6379 shutdown</span><br><span class="line">// 或者使用终极武器</span><br><span class="line">kill -9 杀线程</span><br></pre></td></tr></table></figure>

<h3 id="防火墙配置"><a href="#防火墙配置" class="headerlink" title="防火墙配置"></a>防火墙配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT </span><br><span class="line">/etc/rc.d/init.d/iptables save</span><br><span class="line">/etc/rc.d/init.d/iptables restart</span><br><span class="line">/etc/rc.d/init.d/iptables status</span><br></pre></td></tr></table></figure>

<h3 id="使用Xshell-工具远程连接"><a href="#使用Xshell-工具远程连接" class="headerlink" title="使用Xshell 工具远程连接"></a>使用Xshell 工具远程连接</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379</span><br></pre></td></tr></table></figure>

<h2 id="桌面管理工具"><a href="#桌面管理工具" class="headerlink" title="桌面管理工具"></a>桌面管理工具</h2><p>安装Redis </p>
<h1 id="redis-基本命令"><a href="#redis-基本命令" class="headerlink" title="redis 基本命令"></a>redis 基本命令</h1><h2 id="set-键-值"><a href="#set-键-值" class="headerlink" title="set 键 值"></a>set 键 值</h2><h2 id="get-键"><a href="#get-键" class="headerlink" title="get 键"></a>get 键</h2><h2 id="keys"><a href="#keys" class="headerlink" title="keys *"></a>keys *</h2><table>
<thead>
<tr>
<th>通配符</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>?</td>
<td>匹配一个字符</td>
</tr>
<tr>
<td>*</td>
<td>匹配任意个字符</td>
</tr>
<tr>
<td>[]</td>
<td>匹配括号中, 范围内的任意一个字符</td>
</tr>
<tr>
<td>\</td>
<td>转义字符</td>
</tr>
</tbody></table>
<h2 id="exists-键"><a href="#exists-键" class="headerlink" title="exists 键"></a>exists 键</h2><h2 id="del-键"><a href="#del-键" class="headerlink" title="del 键"></a>del 键</h2><h2 id="type-键"><a href="#type-键" class="headerlink" title="type 键"></a>type 键</h2><h2 id="help"><a href="#help" class="headerlink" title="help"></a>help</h2><h1 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h1><p>redis 多数据库支持</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Redis 默认支持16 个数据库, 命名方式 为 0-15</span><br><span class="line">可以通过参数修改默认数据库的个数</span><br><span class="line">客户端连接 redis 之后 自动选择 0 号数据库</span><br><span class="line">可以通过 select 命令更换数据库 (select 1 )</span><br></pre></td></tr></table></figure>

<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">get set</span><br><span class="line">mset / mget</span><br><span class="line"></span><br><span class="line">incr (自增长)</span><br><span class="line">incrby 指定步长 增长</span><br><span class="line">decr 自减</span><br><span class="line">decrby  </span><br><span class="line">append 向尾部追加值</span><br><span class="line">strlen 获取字符串长度</span><br></pre></td></tr></table></figure>



<p> 管理字符串的基本命令 </p>
<table>
<thead>
<tr>
<th>序号</th>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>SET key value</td>
<td>此命令设置指定键的值。</td>
</tr>
<tr>
<td>2</td>
<td>GET key</td>
<td>获取指定键的值。</td>
</tr>
<tr>
<td>3</td>
<td>GETRANGE key start end</td>
<td>获取存储在键上的字符串的子字符串。</td>
</tr>
<tr>
<td>4</td>
<td>GETSET key value</td>
<td>设置键的字符串值并返回其旧值。</td>
</tr>
<tr>
<td>5</td>
<td>GETBIT key offset</td>
<td>返回在键处存储的字符串值中偏移处的位值。</td>
</tr>
<tr>
<td>6</td>
<td>MGET key1 [key2…]</td>
<td>获取所有给定键的值</td>
</tr>
<tr>
<td>7</td>
<td>SETBIT key offset value</td>
<td>存储在键上的字符串值中设置或清除偏移处的位</td>
</tr>
<tr>
<td>8</td>
<td>SETEX key seconds value</td>
<td>使用键和到期时间来设置值</td>
</tr>
<tr>
<td>9</td>
<td>SETNX key value</td>
<td>设置键的值，仅当键不存在时</td>
</tr>
<tr>
<td>10</td>
<td>SETRANGE key offset value</td>
<td>在指定偏移处开始的键处覆盖字符串的一部分</td>
</tr>
<tr>
<td>11</td>
<td>STRLEN key</td>
<td>获取存储在键中的值的长度</td>
</tr>
<tr>
<td>12</td>
<td>MSET key value [key value …]</td>
<td>为多个键分别设置它们的值</td>
</tr>
<tr>
<td>13</td>
<td>MSETNX key value [key value …]</td>
<td>为多个键分别设置它们的值，仅当键不存在时</td>
</tr>
<tr>
<td>14</td>
<td>PSETEX key milliseconds value</td>
<td>设置键的值和到期时间(以毫秒为单位)</td>
</tr>
<tr>
<td>15</td>
<td>INCR key</td>
<td>将键的整数值增加1</td>
</tr>
<tr>
<td>16</td>
<td>INCRBY key increment</td>
<td>将键的整数值按给定的数值增加</td>
</tr>
<tr>
<td>17</td>
<td>INCRBYFLOAT key increment</td>
<td>将键的浮点值按给定的数值增加</td>
</tr>
<tr>
<td>18</td>
<td>DECR key</td>
<td>将键的整数值减1</td>
</tr>
<tr>
<td>19</td>
<td>DECRBY key decrement</td>
<td>按给定数值减少键的整数值</td>
</tr>
<tr>
<td>20</td>
<td>APPEND key value</td>
<td>将指定值附加到键</td>
</tr>
</tbody></table>
<h2 id="列表类型-List"><a href="#列表类型-List" class="headerlink" title="列表类型(List)"></a>列表类型(List)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[2]&gt; lpush list zhangsan lisi wangwu zhaoliu</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:6379[2]&gt; lrange list 0 10</span><br><span class="line">1) &quot;zhaoliu&quot;</span><br><span class="line">2) &quot;wangwu&quot;</span><br><span class="line">3) &quot;lisi&quot;</span><br><span class="line">4) &quot;zhangsan&quot;</span><br></pre></td></tr></table></figure>

<p>常用命令</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>BLPOP key1<br> [key2 ] timeout</td>
<td>移出并获取列表的第一个元素， <br>如果列表没有元素会阻塞列表<br>直到等待超时或发现可弹出元素为止。</td>
</tr>
<tr>
<td>2</td>
<td>BRPOP key1 [key2 ] timeout</td>
<td>移出并获取列表的最后一个元素， <br>如果列表没有元素会阻塞列表<br>直到等待超时或发现可弹出元素为止。</td>
</tr>
<tr>
<td>3</td>
<td>BRPOPLPUSH source destination timeout</td>
<td>从列表中弹出一个值，<br>将弹出的元素插入到另外一个列表中并返回它； <br>如果列表没有元素会阻塞列表<br>直到等待超时或发现可弹出元素为止。</td>
</tr>
<tr>
<td>4</td>
<td>LINDEX key index</td>
<td>通过索引获取列表中的元素</td>
</tr>
<tr>
<td>5</td>
<td>LINSERT key BEFORE</td>
<td>AFTER pivot value</td>
</tr>
<tr>
<td>6</td>
<td>LLEN key</td>
<td>获取列表长度</td>
</tr>
<tr>
<td>7</td>
<td>LPOP key</td>
<td>移出并获取列表的第一个元素</td>
</tr>
<tr>
<td>8</td>
<td>LPUSH key value1 [value2]</td>
<td>将一个或多个值插入到列表头部</td>
</tr>
<tr>
<td>9</td>
<td>LPUSHX key value</td>
<td>将一个值插入到已存在的列表头部</td>
</tr>
<tr>
<td>10</td>
<td>LRANGE key start stop</td>
<td>获取列表指定范围内的元素</td>
</tr>
<tr>
<td>11</td>
<td>LREM key count value</td>
<td>移除列表元素</td>
</tr>
<tr>
<td>12</td>
<td>LSET key index value</td>
<td>通过索引设置列表元素的值</td>
</tr>
<tr>
<td>13</td>
<td>LTRIM key start stop</td>
<td>对一个列表进行修剪(trim)，<br>就是说，让列表只保留指定区间内的元素，<br>不在指定区间之内的元素都将被删除。</td>
</tr>
<tr>
<td>14</td>
<td>RPOP key</td>
<td>移除并获取列表最后一个元素</td>
</tr>
<tr>
<td>15</td>
<td>RPOPLPUSH source destination</td>
<td>移除列表的最后一个元素，<br>并将该元素添加到另一个列表并返回</td>
</tr>
<tr>
<td>16</td>
<td>RPUSH key value1 [value2]</td>
<td>在列表中添加一个或多个值</td>
</tr>
<tr>
<td>17</td>
<td>RPUSHX key value</td>
<td>为已存在的列表添加值</td>
</tr>
</tbody></table>
<h2 id="set"><a href="#set" class="headerlink" title="set"></a>set</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">	Redis的Set是string类型的无序集合。集合成员是唯一的，这就意味着集合中不能出现重复的数据。</span><br><span class="line">Redis 中 集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。</span><br></pre></td></tr></table></figure>

<p>常用命令</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>SADD key member1 [member2]</td>
<td>向集合添加一个或多个成员</td>
</tr>
<tr>
<td>2</td>
<td>SCARD key</td>
<td>获取集合的成员数</td>
</tr>
<tr>
<td>3</td>
<td>SDIFF key1 [key2]</td>
<td>返回给定所有集合的差集</td>
</tr>
<tr>
<td>4</td>
<td>SDIFFSTORE destination key1 [key2]</td>
<td>返回给定所有集合的差集并存储在 destination 中</td>
</tr>
<tr>
<td>5</td>
<td>SINTER key1 [key2]</td>
<td>返回给定所有集合的交集</td>
</tr>
<tr>
<td>6</td>
<td>SINTERSTORE destination key1 [key2]</td>
<td>返回给定所有集合的交集并存储在 destination 中</td>
</tr>
<tr>
<td>7</td>
<td>SISMEMBER key member</td>
<td>判断 member 元素是否是集合 key 的成员</td>
</tr>
<tr>
<td>8</td>
<td>SMEMBERS key</td>
<td>返回集合中的所有成员</td>
</tr>
<tr>
<td>9</td>
<td>SMOVE source destination member</td>
<td>将 member 元素从 source 集合移动到 destination 集合</td>
</tr>
<tr>
<td>10</td>
<td>SPOP key</td>
<td>移除并返回集合中的一个随机元素</td>
</tr>
<tr>
<td>11</td>
<td>SRANDMEMBER key [count]</td>
<td>返回集合中一个或多个随机数</td>
</tr>
<tr>
<td>12</td>
<td>SREM key member1 [member2]</td>
<td>移除集合中一个或多个成员</td>
</tr>
<tr>
<td>13</td>
<td>SUNION key1 [key2]</td>
<td>返回所有给定集合的并集</td>
</tr>
<tr>
<td>14</td>
<td>SUNIONSTORE destination key1 [key2]</td>
<td>所有给定集合的并集存储在 destination 集合中</td>
</tr>
<tr>
<td>15</td>
<td>SSCAN key cursor [MATCH pattern] [COUNT count]</td>
<td>迭代集合中的元素</td>
</tr>
</tbody></table>
<h3 id="有序集合-sorted-set"><a href="#有序集合-sorted-set" class="headerlink" title="有序集合(sorted set)"></a>有序集合(sorted set)</h3><p>Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。</p>
<p>常用命令</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>常用命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>ZADD key score1 member1 [score2 member2]</td>
<td>向有序集合添加一个或多个成员，或者更新已存在成员的分数</td>
</tr>
<tr>
<td>2</td>
<td>ZCARD key</td>
<td>获取有序集合的成员数</td>
</tr>
<tr>
<td>3</td>
<td>ZCOUNT key min max</td>
<td>计算在有序集合中指定区间分数的成员数</td>
</tr>
<tr>
<td>4</td>
<td>ZINCRBY key increment member</td>
<td>有序集合中对指定成员的分数加上增量 increment</td>
</tr>
<tr>
<td>5</td>
<td>ZINTERSTORE destination numkeys key [key …]</td>
<td>计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中</td>
</tr>
<tr>
<td>6</td>
<td>ZLEXCOUNT key min max</td>
<td>在有序集合中计算指定字典区间内成员数量</td>
</tr>
<tr>
<td>7</td>
<td>ZRANGE key start stop [WITHSCORES]</td>
<td>通过索引区间返回有序集合成指定区间内的成员</td>
</tr>
<tr>
<td>8</td>
<td>ZRANGEBYLEX key min max [LIMIT offset count]</td>
<td>通过字典区间返回有序集合的成员</td>
</tr>
<tr>
<td>9</td>
<td>ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]</td>
<td>通过分数返回有序集合指定区间内的成员</td>
</tr>
<tr>
<td>10</td>
<td>ZRANK key member</td>
<td>返回有序集合中指定成员的索引</td>
</tr>
<tr>
<td>11</td>
<td>ZREM key member [member …]</td>
<td>移除有序集合中的一个或多个成员</td>
</tr>
<tr>
<td>12</td>
<td>ZREMRANGEBYLEX key min max</td>
<td>移除有序集合中给定的字典区间的所有成员</td>
</tr>
<tr>
<td>13</td>
<td>ZREMRANGEBYRANK key start stop</td>
<td>移除有序集合中给定的排名区间的所有成员</td>
</tr>
<tr>
<td>14</td>
<td>ZREMRANGEBYSCORE key min max</td>
<td>移除有序集合中给定的分数区间的所有成员</td>
</tr>
<tr>
<td>15</td>
<td>ZREVRANGE key start stop [WITHSCORES]</td>
<td>返回有序集中指定区间内的成员，通过索引，分数从高到底</td>
</tr>
<tr>
<td>16</td>
<td>ZREVRANGEBYSCORE key max min [WITHSCORES]</td>
<td>返回有序集中指定分数区间内的成员，分数从高到低排序</td>
</tr>
<tr>
<td>17</td>
<td>ZREVRANK key member</td>
<td>返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序</td>
</tr>
<tr>
<td>18</td>
<td>ZSCORE key member</td>
<td>返回有序集中，成员的分数值</td>
</tr>
<tr>
<td>19</td>
<td>ZUNIONSTORE destination numkeys key [key …]</td>
<td>计算给定的一个或多个有序集的并集，并存储在新的 key 中</td>
</tr>
<tr>
<td>20</td>
<td>ZSCAN key cursor [MATCH pattern] [COUNT count]</td>
<td>迭代有序集合中的元素（包括元素成员和元素分值）</td>
</tr>
</tbody></table>
<h2 id="Hash数据结构"><a href="#Hash数据结构" class="headerlink" title="Hash数据结构"></a>Hash数据结构</h2><h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p>Redis hash 是一个string类型的field和value的映射表，hash特别适合用于存储对象。 </p>
<p>Map&lt;String,Map&lt;String,String&gt;&gt; </p>
<p><img src="Redis.assets/2018072900071026.png" alt="è¿éåå¾çæè¿°"> </p>
<h3 id="语法结构"><a href="#语法结构" class="headerlink" title="语法结构"></a>语法结构</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">hset key field value</span><br><span class="line">hset 键  属性 值</span><br><span class="line">对比 </span><br><span class="line">设置 学生键 name属性 值为张三</span><br><span class="line">hget 键 属性</span><br><span class="line">hmset 键 属性 值  [属性 值] ...</span><br><span class="line">hmget 键 属性 [属性]</span><br><span class="line">hgetall key(键)</span><br></pre></td></tr></table></figure>

<h3 id="获取键集或值集"><a href="#获取键集或值集" class="headerlink" title="获取键集或值集"></a>获取键集或值集</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hkeys 键</span><br><span class="line">hvals 键</span><br></pre></td></tr></table></figure>

<h3 id="获取字段数量"><a href="#获取字段数量" class="headerlink" title="获取字段数量"></a>获取字段数量</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hlen 键</span><br></pre></td></tr></table></figure>



<h3 id="判断是否存在"><a href="#判断是否存在" class="headerlink" title="判断是否存在"></a>判断是否存在</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">hsxists 键 属性</span><br><span class="line">127.0.0.1:6379[2]&gt; hset stu name zhangsan</span><br><span class="line">127.0.0.1:6379[2]&gt; hexists stu name</span><br><span class="line">(integer) 1</span><br><span class="line">字段存在 返回 1 真 , 不存在返回 0 假</span><br></pre></td></tr></table></figure>

<h3 id="选择性插入"><a href="#选择性插入" class="headerlink" title="选择性插入"></a>选择性插入</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hsetnx 键 属性 值</span><br><span class="line">如果值存在, 则不执行, 值不存在则执行</span><br></pre></td></tr></table></figure>

<h3 id="自增长"><a href="#自增长" class="headerlink" title="自增长"></a>自增长</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">hincrby per age 1</span><br><span class="line">hincrby key field increment</span><br><span class="line"></span><br><span class="line">hincrby user age 2 (user 键的 age 属性  自增 2)</span><br></pre></td></tr></table></figure>

<h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdel key field [field...]</span><br></pre></td></tr></table></figure>

<p>常用命令汇总</p>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>HDEL key field2 [field2]</td>
<td>删除一个或多个哈希表字段</td>
</tr>
<tr>
<td>2</td>
<td>HEXISTS key field</td>
<td>查看哈希表 key 中，指定的字段是否存在。</td>
</tr>
<tr>
<td>3</td>
<td>HGET key field</td>
<td>获取存储在哈希表中指定字段的值。</td>
</tr>
<tr>
<td>4</td>
<td>HGETALL key</td>
<td>获取在哈希表中指定 key 的所有字段和值</td>
</tr>
<tr>
<td>5</td>
<td>HINCRBY key field increment</td>
<td>为哈希表 key 中的指定字段的整数值加上增量 increment 。</td>
</tr>
<tr>
<td>6</td>
<td>HINCRBYFLOAT key field increment</td>
<td>为哈希表 key 中的指定字段的浮点数值加上增量 increment</td>
</tr>
<tr>
<td>7</td>
<td>HKEYS key</td>
<td>获取所有哈希表中的字段</td>
</tr>
<tr>
<td>8</td>
<td>HLEN key</td>
<td>获取哈希表中字段的数量</td>
</tr>
<tr>
<td>9</td>
<td>HMGET key field1 [field2]</td>
<td>获取所有给定字段的值</td>
</tr>
<tr>
<td>10</td>
<td>HMSET key field1 value1 [field2 value2 ]</td>
<td>同时将多个 field-value (域-值)对设置到哈希表 key 中。</td>
</tr>
<tr>
<td>11</td>
<td>HSET key field value</td>
<td>将哈希表 key 中的字段 field 的值设为 value 。</td>
</tr>
<tr>
<td>12</td>
<td>HSETNX key field value</td>
<td>只有在字段 field 不存在时，设置哈希表字段的值。</td>
</tr>
<tr>
<td>13</td>
<td>HVALS key</td>
<td>获取哈希表中所有值</td>
</tr>
<tr>
<td>14</td>
<td>HSCAN key cursor [MATCH pattern] [COUNT count]</td>
<td>迭代哈希表中的键值对。</td>
</tr>
</tbody></table>
<h1 id="Redis之生存时间"><a href="#Redis之生存时间" class="headerlink" title="Redis之生存时间"></a>Redis之生存时间</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis 一般用于缓存, 而缓存一般都是要设置失效时间的, 到期后数据销毁</span><br></pre></td></tr></table></figure>

<h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">expire key seconds </span><br><span class="line">设置失效时间 键 单位秒</span><br><span class="line">127.0.0.1:6379[2]&gt; set name zhangsan</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[2]&gt; get name </span><br><span class="line">&quot;zhangsan&quot;</span><br><span class="line">127.0.0.1:6379[2]&gt; expire name 5</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[2]&gt; ttl name</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379[2]&gt; ttl name</span><br><span class="line">(integer) -2</span><br><span class="line">127.0.0.1:6379[2]&gt; get name</span><br><span class="line">(nil)</span><br></pre></td></tr></table></figure>



<p>TTL返回值： </p>
<p>大于0的数字：剩余生存时间，单位为秒 </p>
<p>-1 ： 没有生存时间，永久存储 </p>
<p>-2 ： 数据已经被删除 </p>
<h2 id="清除生存时间限制"><a href="#清除生存时间限制" class="headerlink" title="清除生存时间限制"></a>清除生存时间限制</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">persist key</span><br><span class="line">重新设置值, 也会清除生存时间限制</span><br></pre></td></tr></table></figure>

<h1 id="redis-事务"><a href="#redis-事务" class="headerlink" title="redis 事务"></a>redis 事务</h1><p>Redis 事务可以一次执行多个命令， 并且带有以下两个重要的保证：</p>
<p> 事务是一个单独的隔离操作：</p>
<p>​    事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中，不会被其他客户端发送来的命令请求所打断。 </p>
<p>事务是一个原子操作：事务中的命令要么全部被执行，要么全部都不执行。 </p>
<h2 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">MULTI</span><br><span class="line">多条语句代码</span><br><span class="line">EXEC</span><br><span class="line">当 执行 EXEC  以事务的方式 触发命令的执行</span><br></pre></td></tr></table></figure>

<h2 id="常用命令列表"><a href="#常用命令列表" class="headerlink" title="常用命令列表"></a>常用命令列表</h2><table>
<thead>
<tr>
<th>序号</th>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>DISCARD</td>
<td>取消事务，<br>放弃执行事务块内的所有命令。</td>
</tr>
<tr>
<td>2</td>
<td>EXEC</td>
<td>执行所有事务块内的命令。</td>
</tr>
<tr>
<td>3</td>
<td>MULTI</td>
<td>标记一个事务块的开始。</td>
</tr>
<tr>
<td>4</td>
<td>UNWATCH</td>
<td>取消 WATCH 命令对所有 key 的监视。</td>
</tr>
<tr>
<td>5</td>
<td>WATCH key [key …]</td>
<td>监视一个(或多个) key ，<br>如果在事务执行之前<br>这个(或这些) key 被其他命令所改动，<br>那么事务将被打断。</td>
</tr>
</tbody></table>
<h1 id="主从配置"><a href="#主从配置" class="headerlink" title="主从配置"></a>主从配置</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h1 id="Jedis-客户端操作"><a href="#Jedis-客户端操作" class="headerlink" title="Jedis 客户端操作"></a>Jedis 客户端操作</h1><h2 id="Jedis-简介"><a href="#Jedis-简介" class="headerlink" title="Jedis 简介"></a>Jedis 简介</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Jedis是Redis官方推荐的Java连接开发工具。要在Java开发中使用好Redis中间件，必须对Jedis熟悉才能写成漂亮的代码</span><br><span class="line">	Jedis的基本使用非常简单，只需要创建Jedis对象的时候指定host，port, password即可。当然，Jedis对象又很多构造方法，都大同小异，只是对应和Redis连接的socket的参数不一样而已。简单使用如下图所示</span><br></pre></td></tr></table></figure>

<h2 id="导入依赖"><a href="#导入依赖" class="headerlink" title="导入依赖"></a>导入依赖</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">  &lt;groupId&gt;redis.clients&lt;/groupId&gt;</span><br><span class="line">  &lt;artifactId&gt;jedis&lt;/artifactId&gt;</span><br><span class="line">  &lt;version&gt;2.6.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">  &lt;groupId&gt;org.slf4j&lt;/groupId&gt;</span><br><span class="line">  &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;</span><br><span class="line">  &lt;version&gt;1.6.4&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<p>确保 redis 的服务器可以正常访问</p>
<h2 id="代码案例"><a href="#代码案例" class="headerlink" title="代码案例"></a>代码案例</h2><h3 id="基本使用方式"><a href="#基本使用方式" class="headerlink" title="基本使用方式"></a>基本使用方式</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public static void testJedis()&#123;</span><br><span class="line">    Jedis jedis = new Jedis(&quot;192.168.3.186&quot;,6379);</span><br><span class="line">    jedis.set(&quot;jedis&quot;,&quot;hello&quot;);</span><br><span class="line">    String name = jedis.get(&quot;jedis&quot;);</span><br><span class="line">    System.out.println(name);</span><br><span class="line">    jedis.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="连接池方式"><a href="#连接池方式" class="headerlink" title="连接池方式"></a>连接池方式</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public static void testJedisPool()&#123;</span><br><span class="line">    // 构建连接池配置信息</span><br><span class="line">    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();</span><br><span class="line">    // 设置最大连接数</span><br><span class="line">    jedisPoolConfig.setMaxTotal(50);</span><br><span class="line">    // 构建连接池</span><br><span class="line">    JedisPool jedisPool = new JedisPool(jedisPoolConfig, &quot;192.168.3.186&quot;, 6379);</span><br><span class="line">    // 从连接池中获取连接</span><br><span class="line">    Jedis jedis = jedisPool.getResource();</span><br><span class="line">    // 读取数据</span><br><span class="line">    System.out.println(jedis.get(&quot;jedis&quot;));</span><br><span class="line">    // 将连接还回到连接池中</span><br><span class="line">    jedisPool.returnResource(jedis);</span><br><span class="line">    // 释放连接池</span><br><span class="line">    jedisPool.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="spring整合redis"><a href="#spring整合redis" class="headerlink" title="spring整合redis"></a>spring整合redis</h1><h2 id="引入整合包依赖"><a href="#引入整合包依赖" class="headerlink" title="引入整合包依赖"></a>引入整合包依赖</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--redis相关 --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-data-redis&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.8.6.RELEASE&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;redis.clients&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;jedis&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;2.9.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<h2 id="redis-配置文件"><a href="#redis-配置文件" class="headerlink" title="redis 配置文件"></a>redis 配置文件</h2><p>redis.properties </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#redis standalone配置</span><br><span class="line">redis.host=192.168.3.186</span><br><span class="line">redis.port=6379</span><br><span class="line">#redis sentinel配置</span><br><span class="line">#redis 连接池配置</span><br><span class="line">#最大连接数(默认:8)</span><br><span class="line">redis.maxTotal=600</span><br><span class="line">#最大空闲数(默认:8)</span><br><span class="line">redis.maxIdle=300</span><br><span class="line">#当连接池资源耗尽时,调用者最大阻塞时间,超时将抛出异常.单位:毫秒,默认:-1,表示永不超时.</span><br><span class="line">redis.maxWaitMillis=1000</span><br><span class="line">#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 (默认:false)</span><br><span class="line">redis.testOnBorrow=true</span><br><span class="line">redis.timeout=0</span><br></pre></td></tr></table></figure>

<h2 id="自定义-Redis-工具类"><a href="#自定义-Redis-工具类" class="headerlink" title="自定义 Redis 工具类"></a>自定义 Redis 工具类</h2><p>RedisUtil</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line">package cn.bdqn.utils;</span><br><span class="line"></span><br><span class="line">import org.springframework.cache.Cache;</span><br><span class="line">import org.springframework.cache.support.SimpleValueWrapper;</span><br><span class="line">import org.springframework.data.redis.connection.RedisConnection;</span><br><span class="line">import org.springframework.data.redis.core.RedisCallback;</span><br><span class="line">import org.springframework.data.redis.core.RedisTemplate;</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line">import java.util.concurrent.Callable;</span><br><span class="line"></span><br><span class="line">public class RedisUtil implements Cache &#123;</span><br><span class="line"></span><br><span class="line">    private RedisTemplate&lt;String, Object&gt; redisTemplate;</span><br><span class="line">    private String name;</span><br><span class="line"></span><br><span class="line">    public RedisTemplate&lt;String, Object&gt; getRedisTemplate() &#123;</span><br><span class="line">        return redisTemplate;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setRedisTemplate(RedisTemplate&lt;String, Object&gt; redisTemplate) &#123;</span><br><span class="line">        this.redisTemplate = redisTemplate;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setName(String name) &#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getName() &#123;</span><br><span class="line">        return this.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public byte[] toByteArray(Object obj) &#123;</span><br><span class="line">        byte[] bytes = null;</span><br><span class="line">        ByteArrayOutputStream bos = new ByteArrayOutputStream();</span><br><span class="line">        try &#123;</span><br><span class="line">            ObjectOutputStream oos = new ObjectOutputStream(bos);</span><br><span class="line">            oos.writeObject(obj);</span><br><span class="line">            oos.flush();</span><br><span class="line">            bytes = bos.toByteArray();</span><br><span class="line">            oos.close();</span><br><span class="line">            bos.close();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        return bytes;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Object toObject(byte[] bytes) &#123;</span><br><span class="line">        Object obj = null;</span><br><span class="line">        try &#123;</span><br><span class="line">            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);</span><br><span class="line">            ObjectInputStream ois = new ObjectInputStream(bis);</span><br><span class="line">            obj = ois.readObject();</span><br><span class="line">            ois.close();</span><br><span class="line">            bis.close();</span><br><span class="line">        &#125; catch (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        return obj;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public Object getNativeCache() &#123;</span><br><span class="line">        return this.redisTemplate;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public ValueWrapper get(Object key) &#123;</span><br><span class="line">        System.out.println(&quot;--------------------------- get key ----------------------------&quot;);</span><br><span class="line">        final String keyf = key.toString();</span><br><span class="line">        Object object = null;</span><br><span class="line">        object = redisTemplate.execute(new RedisCallback&lt;Object&gt;() &#123;</span><br><span class="line">            public Object doInRedis(RedisConnection connection) &#123;</span><br><span class="line">                byte[] key = keyf.getBytes();</span><br><span class="line">                byte[] value = connection.get(key);</span><br><span class="line">                if (value == null) &#123;</span><br><span class="line">                    return null;</span><br><span class="line">                &#125;</span><br><span class="line">                return toObject(value);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        return (object != null ? new SimpleValueWrapper(object) : null);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void put(Object key, Object value) &#123;</span><br><span class="line">        System.out.println(&quot;------------------- put key --------------------------&quot;);</span><br><span class="line">        final String keyf = key.toString();</span><br><span class="line">        final Object valuef = value;</span><br><span class="line">        final long liveTime = 86400;</span><br><span class="line">        redisTemplate.execute(new RedisCallback&lt;Long&gt;() &#123;</span><br><span class="line">            public Long doInRedis(RedisConnection connection) &#123;</span><br><span class="line">                byte[] keyb = keyf.getBytes();</span><br><span class="line">                byte[] valueb = toByteArray(valuef);</span><br><span class="line">                connection.set(keyb, valueb);</span><br><span class="line">                if (liveTime &gt; 0) &#123;</span><br><span class="line">                    connection.expire(keyb, liveTime);</span><br><span class="line">                &#125;</span><br><span class="line">                return 1L;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void evict(Object key) &#123;</span><br><span class="line">        System.out.println(&quot;-------------------------- del key ---------------------------&quot;);</span><br><span class="line">        final String keyf = key.toString();</span><br><span class="line">        redisTemplate.execute(new RedisCallback&lt;Long&gt;() &#123;</span><br><span class="line">            public Long doInRedis(RedisConnection connection) &#123;</span><br><span class="line">                return connection.del(keyf.getBytes());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void clear() &#123;</span><br><span class="line">        System.out.println(&quot;-------------------- clear key -------------------------&quot;);</span><br><span class="line">        redisTemplate.execute(new RedisCallback&lt;String&gt;() &#123;</span><br><span class="line">            public String doInRedis(RedisConnection connection) &#123;</span><br><span class="line">                connection.flushDb();</span><br><span class="line">                return &quot;ok&quot;;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public ValueWrapper putIfAbsent(Object key, Object value) &#123;</span><br><span class="line">        // TODO Auto-generated method stub</span><br><span class="line">        return null;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public &lt;T&gt; T get(Object key, Class&lt;T&gt; type) &#123;</span><br><span class="line">        // TODO Auto-generated method stub</span><br><span class="line">        return null;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public &lt;T&gt; T get(Object key, Callable&lt;T&gt; valueLoader) &#123;</span><br><span class="line">        // TODO Auto-generated method stub</span><br><span class="line">        return null;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="spring-整合文件"><a href="#spring-整合文件" class="headerlink" title="spring 整合文件"></a>spring 整合文件</h2><p>application-context-redis.xml</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;</span><br><span class="line">       xmlns:redis=&quot;http://www.springframework.org/schema/redis&quot;</span><br><span class="line">       xmlns:cache=&quot;http://www.springframework.org/schema/cache&quot;</span><br><span class="line">       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">       xsi:schemaLocation=&quot;</span><br><span class="line">    http://www.springframework.org/schema/beans</span><br><span class="line">    http://www.springframework.org/schema/beans/spring-beans.xsd</span><br><span class="line">    http://www.springframework.org/schema/context</span><br><span class="line">    http://www.springframework.org/schema/context/spring-context.xsd</span><br><span class="line">    http://www.springframework.org/schema/aop</span><br><span class="line">    http://www.springframework.org/schema/aop/spring-aop.xsd</span><br><span class="line">    http://www.springframework.org/schema/redis</span><br><span class="line">    http://www.springframework.org/schema/redis/spring-redis.xsd</span><br><span class="line">    http://www.springframework.org/schema/cache</span><br><span class="line">    http://www.springframework.org/schema/cache/spring-cache.xsd</span><br><span class="line">    &quot;&gt;</span><br><span class="line">    &lt;context:property-placeholder order=&quot;1&quot; location=&quot;classpath:redis.properties&quot; ignore-unresolvable=&quot;true&quot;/&gt;</span><br><span class="line">    &lt;bean id=&quot;poolConfig&quot; class=&quot;redis.clients.jedis.JedisPoolConfig&quot;&gt;</span><br><span class="line">        &lt;property name=&quot;maxIdle&quot; value=&quot;$&#123;redis.maxIdle&#125;&quot; /&gt;</span><br><span class="line">        &lt;property name=&quot;maxTotal&quot; value=&quot;$&#123;redis.maxTotal&#125;&quot; /&gt;</span><br><span class="line">        &lt;property name=&quot;maxWaitMillis&quot; value=&quot;$&#123;redis.maxWaitMillis&#125;&quot; /&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line"></span><br><span class="line">    &lt;bean id=&quot;jedisConnectionFactory&quot;</span><br><span class="line">          class=&quot;org.springframework.data.redis.connection.jedis.JedisConnectionFactory&quot;&gt;</span><br><span class="line">        &lt;property name=&quot;hostName&quot; value=&quot;$&#123;redis.host&#125;&quot; /&gt;</span><br><span class="line">        &lt;property name=&quot;port&quot; value=&quot;$&#123;redis.port&#125;&quot; /&gt;</span><br><span class="line">        &lt;!--&lt;property name=&quot;database&quot; value=&quot;$&#123;redis.dbIndex&#125;&quot; /&gt;--&gt;</span><br><span class="line">        &lt;property name=&quot;poolConfig&quot; ref=&quot;poolConfig&quot; /&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line"></span><br><span class="line">    &lt;bean id=&quot;redisTemplate&quot;</span><br><span class="line">          class=&quot;org.springframework.data.redis.core.RedisTemplate&quot;&gt;</span><br><span class="line">        &lt;property name=&quot;connectionFactory&quot; ref=&quot;jedisConnectionFactory&quot;/&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line"></span><br><span class="line">    &lt;bean id=&quot;cacheManager&quot; class=&quot;org.springframework.cache.support.SimpleCacheManager&quot;&gt;</span><br><span class="line">        &lt;property name=&quot;caches&quot;&gt;</span><br><span class="line">            &lt;set&gt;</span><br><span class="line">                &lt;!-- 自定义的RedisUtil工具类 --&gt;</span><br><span class="line">                &lt;bean class=&quot;cn.bdqn.utils.RedisUtil&quot;&gt;</span><br><span class="line">                    &lt;property name=&quot;redisTemplate&quot; ref=&quot;redisTemplate&quot;/&gt;</span><br><span class="line">                    &lt;!-- name属性的值自己随便写，等下在注解当中用 --&gt;</span><br><span class="line">                    &lt;property name=&quot;name&quot; value=&quot;common&quot;/&gt;</span><br><span class="line">                &lt;/bean&gt;</span><br><span class="line">            &lt;/set&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    &lt;!-- 开启注解，这个非常重要，否则等下使用缓存注解不生效 --&gt;</span><br><span class="line">    &lt;cache:annotation-driven cache-manager=&quot;cacheManager&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure>

<h2 id="保证配置文件生效"><a href="#保证配置文件生效" class="headerlink" title="保证配置文件生效"></a>保证配置文件生效</h2><h3 id="redis-配置文件能够被加载到"><a href="#redis-配置文件能够被加载到" class="headerlink" title="redis 配置文件能够被加载到"></a>redis 配置文件能够被加载到</h3><p>web.xml 中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 配置 Spring 核心容器 --&gt;</span><br><span class="line">  &lt;context-param&gt;</span><br><span class="line">    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span><br><span class="line">    &lt;param-value&gt;classpath:application-context*.xml&lt;/param-value&gt;</span><br><span class="line">  &lt;/context-param&gt;</span><br></pre></td></tr></table></figure>

<h3 id="检测是否执行SQL语句"><a href="#检测是否执行SQL语句" class="headerlink" title="检测是否执行SQL语句"></a>检测是否执行SQL语句</h3><p>保证验证的时候可以看到SQL语句的打印</p>
<p>mybatis-config.xml</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration</span><br><span class="line">        PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span><br><span class="line">        &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">    &lt;settings&gt;</span><br><span class="line">        &lt;!-- 打印查询语句 --&gt;</span><br><span class="line">        &lt;setting name=&quot;logImpl&quot; value=&quot;STDOUT_LOGGING&quot; /&gt;</span><br><span class="line">    &lt;/settings&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure>

<h3 id="spring-加载-该配置文件"><a href="#spring-加载-该配置文件" class="headerlink" title="spring 加载 该配置文件"></a>spring 加载 该配置文件</h3><p>application-context-dao.xml</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- myBatis文件 手动加载配置文件  --&gt;</span><br><span class="line">&lt;bean id=&quot;sqlSessionFactory&quot; class=&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;&gt;</span><br><span class="line">    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;configLocation&quot; value=&quot;classpath:mybatis-config.xml&quot;/&gt;</span><br><span class="line">    &lt;!-- 配置数据库表对应的java实体类 --&gt;</span><br><span class="line">    &lt;property name=&quot;typeAliasesPackage&quot; value=&quot;cn.bdqn.pojo&quot; /&gt;</span><br><span class="line">    &lt;!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --&gt;</span><br><span class="line">    &lt;property name=&quot;mapperLocations&quot; value=&quot;classpath:/mapper/*.xml&quot; /&gt;</span><br><span class="line">&lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Service</span><br><span class="line">public class PhoneService &#123;</span><br><span class="line">    @Autowired // 自动注入 (告诉Spring 我需要一个 PhoneMapper )</span><br><span class="line">    private PhoneMapper phoneMapper;</span><br><span class="line"></span><br><span class="line">    @Cacheable(value=&quot;common&quot;) //加入缓存</span><br><span class="line">    //@CacheEvict(value=&quot;common&quot;) //清除缓存</span><br><span class="line">    public List&lt;Phone&gt; selectAll()&#123;</span><br><span class="line">        return phoneMapper.selectAll();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>第一次访问该方法的时候能清晰的看到sql 语句的执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">--------------------------- get key ----------------------------</span><br><span class="line">Creating a new SqlSession</span><br><span class="line">SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@57476647] was not registered for synchronization because synchronization is not active</span><br><span class="line">JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7469a8ba] will not be managed by Spring</span><br><span class="line">==&gt;  Preparing: SELECT * FROM phone </span><br><span class="line">==&gt; Parameters: </span><br><span class="line">&lt;==    Columns: pId, name, gender, birthday, phonenum, address, type, photo</span><br><span class="line">&lt;==        Row: 1, 张三, 0, 2008-08-08, 16868686868, 青岛, 朋友, D:\photo\1.jpg</span><br><span class="line">&lt;==        Row: 2, 李四, 1, 2008-08-09, 1868686868686, 济南, 妹妹, D:\photo\2.jpg</span><br><span class="line">&lt;==      Total: 2</span><br><span class="line">Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@57476647]</span><br><span class="line">------------------- put key --------------------------</span><br></pre></td></tr></table></figure>

<p>第二次执行该方法, 不会打印sql 语句</p>
<p>缓存设置生效</p>
<h1 id="SpringBoot-整合-Redis"><a href="#SpringBoot-整合-Redis" class="headerlink" title="SpringBoot 整合 Redis"></a>SpringBoot 整合 Redis</h1><h2 id="添加依赖"><a href="#添加依赖" class="headerlink" title="添加依赖"></a>添加依赖</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">  &lt;artifactId&gt;spring-boot-starter-redis&lt;/artifactId&gt;</span><br><span class="line">  &lt;version&gt;1.4.7.RELEASE&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<h2 id="配置文件配置redis-参数"><a href="#配置文件配置redis-参数" class="headerlink" title="配置文件配置redis 参数"></a>配置文件配置redis 参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"># redis</span><br><span class="line">spring.redis.host=192.168.3.186</span><br><span class="line">spring.redis.port=6379</span><br><span class="line">#连接池最大连接数</span><br><span class="line">spring.redis.jedis.pool.max-active=8</span><br><span class="line">#最小空闲连接</span><br><span class="line">spring.redis.jedis.pool.min-idle=0</span><br><span class="line">#最大阻塞等待时间，负值表示没有限制</span><br><span class="line">spring.redis.jedis.pool.max-wait=-1</span><br><span class="line">#最大空闲连接</span><br><span class="line">spring.redis.jedis.pool.max-idle=8</span><br><span class="line">#连接超时时间(毫秒)</span><br><span class="line">spring.redis.timeout=20</span><br></pre></td></tr></table></figure>

<h2 id="开启缓存"><a href="#开启缓存" class="headerlink" title="开启缓存"></a>开启缓存</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@EnableCaching</span><br><span class="line">@SpringBootApplication</span><br><span class="line">@MapperScan(&quot;cn.bdqn.mapper&quot;)</span><br><span class="line">public class App &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        SpringApplication.run(App.class,args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="保存返回值到缓存"><a href="#保存返回值到缓存" class="headerlink" title="保存返回值到缓存"></a>保存返回值到缓存</h2><p>service 层代码 演示</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Cacheable(value = &quot;common&quot;)</span><br><span class="line">//@CacheEvict(value=&quot;common&quot;) //清除缓存</span><br><span class="line">public List&lt;Phone&gt; selectAll()&#123;</span><br><span class="line">    return phoneMapper.selectAll();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
















  </article>
</div>


    




</div>

<div class="footer-wrapper container">
  <footer class="footer clearfix">
    <div class="clearfix">
    <a href="http://weixin66.gitee.io/blog" class="footer-logo">
      <i class="fa fa-github"></i>
    </a>
    <ul class="footer-social-link">
      <li>© 2019 矫情</li>
      <li><a href="http://weixin66.gitee.io/blog">Home</a></li>
      
    </ul>
    <div class="footer-theme-info">
      Theme <a href="//github.com/sabrinaluo/hexo-theme-replica">Replica</a>
      by <a href="//github.com/sabrinaluo">Hiitea</a> ❤ Powered by Hexo
    </div>
    </div>
    
  </footer>
</div>




<script src="/blog/js/main.js"></script>

</body>
</html>
